Skip to content

Audit and fix scheduling and state management#15

Merged
luke-lombardi merged 11 commits into
mainfrom
cursor/audit-and-fix-scheduling-and-state-management-claude-4.5-opus-high-thinking-2e1c
Nov 26, 2025
Merged

Audit and fix scheduling and state management#15
luke-lombardi merged 11 commits into
mainfrom
cursor/audit-and-fix-scheduling-and-state-management-claude-4.5-opus-high-thinking-2e1c

Conversation

@luke-lombardi
Copy link
Copy Markdown
Contributor

Fix scheduling bugs and improve state management for reliable wake-up times and overall code quality.

The primary goal was to ensure reliable sleep_for and sleep_until functionality. This involved fixing a critical bug where sub-second sleep durations were lost due to integer casting in Redis TTLs, and resolving an inconsistency in how wake keys were managed in the Redis wake index, which led to orphaned entries. Additionally, various state management inconsistencies and code quality issues were addressed across the codebase.


Open in Cursor Open in Web

This commit introduces several improvements:

- **Enhanced `LoopContext.sleep_for` and `LoopContext.sleep_until`**:
    - Added docstrings explaining functionality, arguments, and potential errors.
    - Implemented validation for sleep durations and timestamps to prevent invalid values.
    - `sleep_for` now raises a `ValueError` if the duration is not positive.
    - `sleep_until` now raises a `ValueError` if the timestamp is in the past.

- **State Management Updates**:
    - **`LoopEvent` and `LoopState`**:
        - Renamed `to_json` methods to `to_dict` for clarity and consistency.
        - Added `to_string` methods for JSON serialization.
        - Kept `to_json` as a deprecated alias for `to_dict` for backward compatibility.
        - Changed `created_at` type in `LoopState` from `int` to `float` for better precision.
    - **`RedisStateManager`**:
        - Improved `_run_wake_monitoring` with better error handling and logging.
        - Modified `set_wake_time` to use milliseconds (`px`) for Redis TTL, allowing for more precise wake-up scheduling.
        - Updated `set_wake_time` to use the full wake key in `srem` for consistency.
        - Added a docstring to `has_claim` for better understanding.
    - **`S3StateManager`**:
        - Explicitly raised `NotImplementedError` in `set_wake_time` with a clear message indicating that S3 does not support wake time scheduling.
        - Added a private `_delete_object` method.

- **Utility Function Update**:
    - **`infer_application_path`**:
        - Updated the function signature to accept `app_instance` instead of `self`.
        - Improved the docstring to clarify its purpose and arguments.
        - Enhanced introspection logic to correctly identify the application instance.

Co-authored-by: luke <luke@smartshare.io>
@cursor
Copy link
Copy Markdown

cursor Bot commented Nov 26, 2025

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

cursoragent and others added 10 commits November 26, 2025 14:35
Co-authored-by: luke <luke@smartshare.io>
Co-authored-by: luke <luke@smartshare.io>
Co-authored-by: luke <luke@smartshare.io>
Co-authored-by: luke <luke@smartshare.io>
This commit replaces fakeredis with a real Redis instance for scheduling tests. It also updates the CI to include a Redis service and modifies the Makefile to support starting/stopping a local Redis instance. The integration tests have been removed as they are now covered by the refactored unit tests.

Co-authored-by: luke <luke@smartshare.io>
Co-authored-by: luke <luke@smartshare.io>
This commit refactors the state management system, particularly for Redis, to enhance reliability and simplify the code. It also updates documentation and tests for better clarity and robustness.

Co-authored-by: luke <luke@smartshare.io>
Add stop method to RedisStateManager for graceful shutdown of the wake monitoring thread.
Update tests to use unique app names and ensure proper cleanup.
Add pytest-asyncio and ruff to dev dependencies.
Update fastapi version to 0.115.14.
Update fastloop version to 0.1.71.

Co-authored-by: luke <luke@smartshare.io>
Co-authored-by: luke <luke@smartshare.io>
Co-authored-by: luke <luke@smartshare.io>
@luke-lombardi luke-lombardi merged commit 38148e9 into main Nov 26, 2025
2 checks passed
@luke-lombardi luke-lombardi deleted the cursor/audit-and-fix-scheduling-and-state-management-claude-4.5-opus-high-thinking-2e1c branch November 26, 2025 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants